package fnic.prehand.esagent.activity_measure;

import java.util.List;

import org.apache.log4j.Logger;

import fnic.prehand.common.AgentConfig;
import fnic.river.RedisRiver;
import fnic.river.RedisSingleLoop;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/*
 * 多线程方式实现
 */
public class ActiveRedisRiver extends RedisRiver implements RedisSingleLoop, Runnable{
	private static final AgentConfig config = AgentConfig.getInstance();
	private ProbeAgent probeAgent;
	private List<String> response = null;
	
	public ActiveRedisRiver(String idx, String type, String redisIp, int redisPort, String redisky, String ctrlUrl, String skUrl, String pbAddrUrl){
		super(redisIp, redisPort, redisky);
		probeAgent = new ProbeAgent(idx, type, ctrlUrl, skUrl, pbAddrUrl);
	}
	
	public void run() {
		while (true) {
			 loopSingle();
			 probeAgent.processBulkIfNeeded(true);
		}
	}

	public void loopSingle() {
		int counter = 0;
		do {
			response = null;
			try {
				response = jedis.blpop(redisbulkTimeout, redisKey);
			} catch (Exception e) {
				// Can't get a redis object. Return and try again on th next loop.
				logger.info("Can't read from redis. Waiting 5 seconds and trying again.");
			}

			if (response != null) {
				String jsonsrc = response.get(1);
				dealRecord(jsonsrc);
			}
		} while (++counter < redisoncesize && response != null);
		cycle++;
	}
	

	public void dealRecord(String item) {
		// TODO Auto-generated method stub
		probeAgent.atomProcess(item);
	}
}
